package us.hootpic.client.presenter;

import java.util.ArrayList;
import java.util.List;

import us.hootpic.client.helper.ClickPoint;
import us.hootpic.client.helper.RPCCall;
import us.hootpic.client.service.PhotoServiceAsync;
import us.hootpic.shared.PhotoDTO;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.event.shared.SimpleEventBus;

public class PhotoListPresenter implements Presenter{
	public interface Display{
		HasClickHandlers getPhotoList();
		ClickPoint getClickedPoint(ClickEvent event);
		int getClickedRow(ClickEvent event);
		Widget asWidget();
		void setData(List<String> photoName);
	}
	
	private List<PhotoDTO> photoList;
	
	public List<PhotoDTO> getPhotoList() {
		return photoList;
	}

	public void setPhotoList(List<PhotoDTO> photoList) {
		this.photoList = photoList;
	}

	private final Display display;
	private final PhotoServiceAsync rpcService;
	private final SimpleEventBus eventBus;
	
	
	  public PhotoListPresenter(PhotoServiceAsync rpcService, SimpleEventBus eventBus, Display view) {
		    this.rpcService = rpcService;
		    this.eventBus = eventBus;
		    this.display = view;
		    bind();
		  }	
	
	public void bind() {
		if(display.getPhotoList() != null){
			display.getPhotoList().addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					int propClicked = display.getClickedRow(event);
					
					GWT.log("Photo List Clicked");
					
					if(propClicked >= 0){
						GWT.log("Photo list property button clicked");
					}else{
						GWT.log("Photo list CheckBox clicked");
					}
					
				}
			});
		}
	}// bind
	
	public void go(final HasWidgets container) {
	    container.clear();
	    container.add(display.asWidget());
	    fetchPhotoListDTO();
	 }	

	 private void fetchPhotoListDTO() {

		    new RPCCall<ArrayList<PhotoDTO>>() {
		      @Override
		      protected void callService(AsyncCallback<ArrayList<PhotoDTO>> cb) {
		        rpcService.getPhotoList(cb);
		      }

		      @Override
		      public void onSuccess(ArrayList<PhotoDTO> result) {
		    	  photoList = result;
		        //sortPhotoDTO();
		    	 
		        display.setData(toStringList(photoList));
		        //fireFriendListChangeEvent();
		      }

		      @Override
		      public void onFailure(Throwable caught) {
		        Window.alert("Error fetching friend summaries: "
		            + caught.getMessage());
		      }
		    }.retry(3);

		  }	
	 
	  private List<String> toStringList(List<PhotoDTO> friendSummaries) {
		    List<String> list = new ArrayList<String>();
		    for (PhotoDTO f : photoList) {
		      list.add(f.getTitle());
		    }
		    return list;
		  }	 
}
